import Vue from 'vue'
import Router from 'vue-router'
import { Message } from 'element-ui'
import { getToken } from '@/utils/auth'

Vue.use(Router)

import Layout from '@/layout'

// 所有路由配置，这些配置同时也是侧边栏菜单的数据来源
export const constantRoutes = [
  // 登录路由，hidden为true表示不在侧边栏中显示
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  // 404页面路由
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 用户中心路由，不在侧边栏中显示
  {
    path: '/userCenter',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'UserCenter',
        component: () => import('@/views/userCenter/index'),
        meta: { title: '个人中心' }
      }
    ]
  },


  // 首页路由，显示在侧边栏的第一个位置
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'el-icon-s-home' } // 标题和图标在侧边栏中显示
    }]
  },

  // 班级学员管理模块，有子菜单
  {
    path: '/example',
    component: Layout,
    redirect: '/example',
    name: 'Example',
    meta: { title: '班级学员管理', icon: 'el-icon-s-help' }, // 父级菜单标题和图标
    children: [
      {
        path: 'classes',
        name: 'Classes',
        component: () => import('@/views/classes'),
        meta: { title: '班级管理', icon: 'el-icon-menu' } // 子菜单标题和图标
      },
      {
        path: 'student',
        name: 'Student',
        component: () => import('@/views/student'),
        meta: { title: '学员管理', icon: 'el-icon-user-solid' }
      }
    ]
  }, 
  // 系统信息管理模块，有子菜单
  {
    path: '/system',
    component: Layout,
    redirect: '/system',
    name: 'System',
    meta: { title: '系统信息管理', icon: 'el-icon-s-tools' }, // 父级菜单标题和图标
    children: [
      {
        path: 'dept',
        name: 'Dept',
        component: () => import('@/views/dept'),
        meta: { title: '部门管理', icon: 'el-icon-menu' }
      },
      {
        path: 'emp',
        name: 'Emp',
        component: () => import('@/views/emp'),
        meta: { title: '员工管理', icon: 'el-icon-user-solid' }
      }
    ]
  },
  // 数据统计管理模块，有子菜单
  {
    path: '/report',
    component: Layout,
    redirect: '/report',
    name: 'Report',
    meta: { title: '数据统计管理', icon: 'el-icon-s-tools' }, // 父级菜单标题和图标
    children: [
      {
        path: 'emp-report',
        name: 'emp-report',
        component: () => import('@/views/emp-report'),
        meta: { title: '员工信息统计', icon: 'el-icon-s-data' }
      },
      {
        path: 'student-report',
        name: 'student-report',
        component: () => import('@/views/student-report'),
        meta: { title: '学员信息统计', icon: 'el-icon-s-data' }
      }
    ]
  },
  // 公告列表模块，无子菜单但使用了与其他模块相同的布局结构
  {
    path: '/message',
    component: Layout,
    redirect: '/message',
    // meta: { title: '公告管理', icon: 'el-icon-s-tools' },
    children: [{
      path: 'messageList',
      name: 'messageList',
      component: () => import('@/views/messageList'),
      meta: { title: '公告列表', icon: 'el-icon-s-comment' } // 菜单标题和图标
    }]
  },

  // 课程管理模块，无子菜单
  {
    path: '/course',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Course',
        component: () => import('@/views/course/index'),
        meta: { title: '课程管理', icon: 'el-icon-notebook-2' } // 菜单标题和图标
      }
    ]
  },

  // 日志管理模块
  {
    path: '/form',
    component: Layout,
    redirect: '/log',
    children: [{
      path: 'log',
      name: 'operationLog',
      component: () => import('@/views/log'),
      meta: { title: '日志管理', icon: 'el-icon-s-comment' } // 菜单标题和图标
    }]
  },
  // AI聊天室模块
  {
    path: '/chat',
    component: Layout,
    children: [
      {
        path: 'chat',
        name: 'chat',
        component: () => import('@/views/chat/index'),
        meta: { title: 'AI聊天室', icon: 'el-icon-chat-dot-round' } // 菜单标题和图标
      }
    ]
  },
  // 404页面必须放在最后，作为未匹配路由的兜底
  { path: '*', redirect: '/404', hidden: true }
]

// 创建路由实例
const createRouter = () => new Router({
  mode: 'history', // 需要服务器支持
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 添加导航守卫
router.beforeEach((to, from, next) => {
  const token = getToken()

  if (to.path === '/login') {
    next()
    return
  }

  if (!token) {
    Message({
      message: '请先登录',
      type: 'warning',
      duration: 2000
    })
    next('/login')
    return
  }

  next()
})

// 重置路由功能，用于动态更改路由时使用
// 详情见: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重置路由
}

export default router
